package br.com.atid.view.controls
{
	import br.com.atid.model.constants.Constants;
	import br.com.atid.util.Utils;
	import br.com.atid.view.graphics.assets.cursors.CursorAssets;
	import br.com.atid.view.graphics.assets.icons.IconAssets;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.TabNavigator;
	import mx.controls.Alert;
	import mx.controls.MenuBar;
	import mx.controls.ToolTip;
	import mx.core.Application;
	import mx.events.CloseEvent;
	import mx.events.MenuEvent;
	import mx.managers.CursorManager;
	import mx.managers.PopUpManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.remoting.RemoteObject;
	import mx.skins.halo.ToolTipBorder;
	import mx.utils.OnDemandEventDispatcher;
	
	import spark.components.BorderContainer;
	import spark.components.Button;
	import spark.components.TitleWindow;
	import spark.components.VGroup;
	import spark.events.PopUpEvent;
	
	import valueObjects.Net;
	import valueObjects.Teacher;

	public class MainMenu extends MenuBar
	{
		public static const _REMOTE_SERVICE:String="ATID_Facade";
		public static const _FACADE_OBJECT:String="facadeObject";

		public var createNewNetForm:NewNetCreator;
		public var savedNetsList:SavedNetsList;
		
		public function MainMenu()
		{
			fileMenuItem=new MenuItemWithChildren(Constants._FILE, true, "normal", true, "", null);
			fileMenuItem.children.push(new MenuItem(Constants._NEW, true, "normal", true, "", IconAssets.newIcon, "Create new activities net"));
			fileMenuItem.children.push(new MenuItem(Constants._OPEN, true, "normal", true, "", IconAssets.openIcon, "Open a previously saved activities net"));
			fileMenuItem.children.push(new MenuItem("", false, "separator"));
			fileMenuItem.children.push(new MenuItem(Constants._CLOSE, false, "normal", true, "", IconAssets.closeIcon, "Close the current activities net"));
			fileMenuItem.children.push(new MenuItem(Constants._CLOSE_ALL, false, "normal", true, "", IconAssets.closeIcon, "Close all activities nets"));
			fileMenuItem.children.push(new MenuItem("", false, "separator"));
			fileMenuItem.children.push(new MenuItem(Constants._SAVE, true, "normal", true, "", IconAssets.saveIcon, "Save the current activities net"));
			///fileMenuItem.children.push(new MenuItem("", false, "separator"));
			//fileMenuItem.children.push(new MenuItem(Constants._PRINT, true, "normal", true, "", IconAssets.printIcon, "Print the currente activities net"));
			_menuData.push(fileMenuItem);

			var viewMenuItem:MenuItemWithChildren=new MenuItemWithChildren(Constants._VIEW, true, "normal", true, "", null);
			viewMenuItem.children.push(new MenuItem(Constants._FULL_SCREEN, true, "normal", true, "", null, "Toggle between fullscreen e standard view"));
			_menuData.push(viewMenuItem);

			var drawMenuItem:MenuItemWithChildren=new MenuItemWithChildren(Constants._DRAW, false, "normal", true, "", null);
			drawMenuItem.children.push(new MenuItem(Constants._SELECT, true, "normal", true, "", IconAssets.selectIcon, "Selection Tool"));
			drawMenuItem.children.push(new MenuItem(Constants._OPEN_HAND, true, "normal", true, "", IconAssets.openedHandIcon, "Hand Tool"));
			drawMenuItem.children.push(new MenuItem(Constants._ZOOM, true, "normal", true, "", IconAssets.zoomIcon, "Zoom Tool"));
			drawMenuItem.children.push(new MenuItem("", false, "separator"));
			drawMenuItem.children.push(new MenuItem(Constants._ACTIVITY, true, "normal", true, "", IconAssets.activityIcon, "Activity Tool"));
			drawMenuItem.children.push(new MenuItem(Constants._COMPOSED_ACTIVITY, true, "normal", true, "", IconAssets.composedActivityIcon, "Composed Activity Tool"));
			drawMenuItem.children.push(new MenuItem(Constants._TRANSITION, true, "normal", true, "", IconAssets.transitionIcon, "Transition Tool"));
			drawMenuItem.children.push(new MenuItem(Constants._ARC, true, "normal", true, "", IconAssets.arcIcon, "Arc Tool"));
			drawMenuItem.children.push(new MenuItem(Constants._EVENT, true, "normal", true, "", IconAssets.eventIcon, "Event Tool"));
			drawMenuItem.children.push(new MenuItem(Constants._REPOSITORY, true, "normal", true, "", IconAssets.repositoryIcon, "Repository Tool"));
			drawMenuItem.children.push(new MenuItem("", false, "separator"));
			drawMenuItem.children.push(new MenuItem(Constants._DELETE, true, "normal", true, "", IconAssets.deleteIcon, "Delete Tool"));
			_menuData.push(drawMenuItem);

			var helpMenuItem:MenuItemWithChildren=new MenuItemWithChildren("Help", true);
			//helpMenuItem.children.push(new MenuItem("Help", true));
			_menuData.push(helpMenuItem);

			this.dataProvider=_menuData;

			//roFacade.destination = _REMOTE_SERVICE;
			//roFacade.source = _FACADE_OBJECT;
			//roFacade.addEventListener(FaultEvent.FAULT, onFault);	

			//roFacade.createNet.addEventListener(FaultEvent.FAULT, onFault);
			//roFacade.createNet.addEventListener("result", submitData);
			//roFacade.createNet.addEventListener("fault", onFault);

		}

		protected function onFault(event:FaultEvent):void
		{
			Alert.show(event.toString());
		}

		//[Bindable]
		//public var selectedTool:String;

		private var _menuData:Array=[];

		[Bindable]
		private var appTabNavigator:TabNavigator;
		private var fileMenuItem:MenuItemWithChildren;

		//private var roFacade:RemoteObject=new RemoteObject();


		public function change(event:MenuEvent):void
		{

		}


		public function createNewTab():void
		{
			//roFacade.createTransition("EEE");
			//roFacade.createTeacher("Pedro");
			//roFacade.createNet(new Date(), new Date());

			//roFacade.createNet(createNewNetForm.dfStart.selectedDate, createNewNetForm.dfEnd.selectedDate);
			//displayNewNetForm();


			appTabNavigator=TabNavigator(this.parentApplication.tabNavigator);
			var newTadDrawArea:TabDrawArea=new TabDrawArea();
			newTadDrawArea.label=createNewNetForm.currentNet.title_LMS;
			newTadDrawArea.id="tab" + (appTabNavigator.numChildren + 1);
			appTabNavigator.addElement(newTadDrawArea);
			appTabNavigator.selectedIndex=appTabNavigator.numChildren - 1;

			if (!appTabNavigator.visible)
			{
				appTabNavigator.visible=true;
					//this.parentApplication.updateControls();
			}

			enabledDrawMenu();
			this.enabledDisableCloseButtons(true);
		}

		public function enabledDisableCloseButtons(pEnabled:Boolean):void
		{

			var menuItemClose:MenuItem=fileMenuItem.children[3] as MenuItem;
			menuItemClose.enabled=pEnabled;

			var menuItemCloseAll:MenuItem=fileMenuItem.children[4] as MenuItem;
			menuItemCloseAll.enabled=pEnabled;
		}

		public function enabledDrawMenu():void
		{
			var menuItemDraw:MenuItem=_menuData[2] as MenuItemWithChildren;
			menuItemDraw.enabled=true;
		}

		public function get menuData():Array
		{
			return _menuData;
		}

		public function openAlertClose(eventObj:CloseEvent):void
		{
			if (eventObj.detail == Alert.OK)
			{
				closeSelectedTab();
					//this.parentApplication.updateControls();
			}
		}

		public function closeSelectedTab():void
		{
			if (appTabNavigator != null)
			{
				appTabNavigator.removeElementAt(appTabNavigator.selectedIndex);
				appTabNavigator.visible=appTabNavigator.numChildren >= 1;
				if (!appTabNavigator.visible)
				{
					this.enabledDisableCloseButtons(false);
//					statusBar.stsLabel1.text="";
				}
			}
		}

		public function displayNewNetForm():void
		{
			createNewNetForm=NewNetCreator(PopUpManager.createPopUp(this, NewNetCreator, true));
			createNewNetForm.x=(stage.stageWidth / 2) - (createNewNetForm.width / 2);
			createNewNetForm.y=(stage.stageHeight / 2) - (createNewNetForm.height / 2);
			createNewNetForm.addEventListener(Event.REMOVED, onCreateNewNetForm);
		}

		protected function onCreateNewNetForm(event:Event):void
		{
			if (event.target is NewNetCreator)
			{
				if (createNewNetForm.currentNet) {
					this.parentApplication.app.mainCurrentNet = createNewNetForm.currentNet;
					//this.parentApplication.currentTeacher = createNewNetForm.ddlTeachers.selectedItem as Teacher;
					createNewTab();
				}
			}
		}

		public function displaySavedNetsForm():void
		{
			savedNetsList=SavedNetsList(PopUpManager.createPopUp(this, SavedNetsList, true));
			savedNetsList.addEventListener("close", removeSavedNetsList);
			//savedNetsList.cancelButton.addEventListener("click", removeMe);
			//savedNetsList.okButton.addEventListener("click", submitData);
			savedNetsList.x=(stage.stageWidth / 2) - (savedNetsList.width / 2);
			savedNetsList.y=(stage.stageHeight / 2) - (savedNetsList.height / 2);
		}

		protected function removeSavedNetsList(event:CloseEvent):void
		{
			PopUpManager.removePopUp(savedNetsList);
		}


	/* OK button click event listener.
	Sets the target component in the application to the
	selected ComboBox item value. */
//		private function submitData(event:Event):void
//		{
//			//roFacade.createNet(createNewNetForm.dfStart.selectedDate, createNewNetForm.dfEnd.selectedDate);
//			removeCreateNewNet(event);
//			createNewTab();
//
//		}

	}
}
